<template>
  <el-dialog
    :title="!dataForm.transportId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="标题" prop="title">
        <el-input v-model="dataForm.title" placeholder="标题"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input v-model="dataForm.content" placeholder="内容"></el-input>
      </el-form-item>
      <el-form-item label="序号" prop="seq">
        <el-input-number
          v-model="dataForm.seq"
          controls-position="right"
          :min="0"
          :max="10"
        ></el-input-number>
      </el-form-item>
      <el-form-item label="状态" size="mini" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="0">下线</el-radio>
          <el-radio :label="1">正常</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { Debounce } from "@/utils/debounce";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        content: "",
        transportId: "",
        recDate: "",
        seq: "",
        shopId: 1,
        status: "",
        title: "",
      },
      page: {
        total: 0, // 总页数
        currentPage: 1, // 当前页数
        pageSize: 10, // 每页显示多少条
      },
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      dataRule: {
        content: [
          { required: true, message: "用户名不能为空", trigger: "blur" },
        ],
        title: [{ required: true, message: "用户名不能为空", trigger: "blur" }],
      },
    };
  },
  methods: {
    init(id) {
      this.dataForm.transportId = id || 0;
      this.visible = true;
      this.$nextTick(() => {
        this.$refs.dataForm.resetFields();
      });
      this.dataForm.recDate=this.time();
      if (this.dataForm.transportId) {
        this.$http({
          url: this.$http.adornUrl(
            `/admin/hotSearch/info/${this.dataForm.transportId}`
          ),
          method: "get",
          params: this.$http.adornParams(),
        }).then(({ data }) => {
          this.dataForm = data;
          console.log(data);
        });
      }
    },
    // 表单提交
    dataFormSubmit: Debounce(function () {
      this.$refs["dataForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/admin/hotSearch`),
            method: this.dataForm.transportId ? "put" : "post",
            data: this.$http.adornData(this.dataForm),
            // {
            //   userId: this.dataForm.userId || undefined,
            //   nickName: this.dataForm.nickName,
            //   status: this.dataForm.status,
            // }
          }).then(({ data }) => {
            this.$message({
              message: "操作成功",
              type: "success",
              duration: 1500,
              onClose: () => {
                this.visible = false;
                this.$emit("refreshDataList", this.page);
              },
            });
          });
        }
      });
    }),
    time() {
      let date = new Date();
      let year = date.getFullYear();
      let month = date.getMonth() + 1;
      if(month.length<1){
        month=`0${month}`
      }
      let day = date.getDate();
      let hours = date.getHours();
      let minu = date.getMinutes();
      let seco = date.getSeconds();
      return `${year}-${month}-${day} ${hours}:${minu}:${seco}`
    },
  },
};
</script>
